﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Zhuang.WebSite.Areas.SystemManagement.Models;
using System.Data;
using Zhuang.Utility.Web.JSON;
using Zhuang.WebSite.BLL.SystemManagement;
using Zhuang.Model.EF;

namespace Zhuang.WebSite.Areas.SystemManagement.Controllers
{
    public class UserController : BaseController
    {
        //
        // GET: /SystemManagement/User/

        Sys_UserBLL sys_userBLL = new Sys_UserBLL();

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GetUser()
        {
            string conditions = Request.QueryString["conditions"] ?? "";
            string page = Request.Params["page"];
            string pagesize = Request.Params["pagesize"];
            string sortName = Request.Params["sortname"];
            string sortOrder = Request.Params["sortorder"];

            int totalRowCount = 0;

            //conditions = "女"; 

            
            DataTable dtUser;

            
            //dtUserInfo = ui.getAllUserInfo();
            dtUser = sys_userBLL.GetPageOfUser(Convert.ToInt32(page.Trim()), Convert.ToInt32(pagesize.Trim()), ref totalRowCount, conditions);


            string strSort = sortName + " " + sortOrder;

            DataTable dtUserSorted = dtUser.Clone();


            DataRow[] drs = dtUser.Select("", strSort);


            foreach (DataRow dr in drs)
            {

                dtUserSorted.ImportRow(dr);
            }

            string strJson = JsonDataHelper.DataTableToJSON(dtUserSorted);
            //Response.Write(json);
            strJson = "{Rows:" + strJson + ",Total:" + totalRowCount + "}";

            //Response.Write(json);
            //return Json(json, JsonRequestBehavior.AllowGet);

            return Content(strJson);
            //return Json(ui.getAllUserInfo(), JsonRequestBehavior.AllowGet);
        }


        public ActionResult UserEdit(string id)
        {

            Sys_User uie = sys_userBLL.GetEntity(Convert.ToInt32(id));
            //ViewData["uie"] = uie;
            return View(uie);
        }

        public ActionResult UserAdd()
        {
           
            Sys_User uie = new Sys_User();
            uie.UserId = -1;
            uie.Gender = "男";
            //ViewData["uie"] = uie;
            return View("UserEdit", uie);
        }

        [HttpPost]
        public ActionResult SaveUser()
        {
            Sys_User uie = new Sys_User();
            uie.UserId = Convert.ToInt32(Request.Form["userId"]);
            uie.UserName = Request.Form["userName"];
            uie.Password = Request.Form["password"];
            uie.Name = Request.Form["name"];
            uie.Gender = Request.Form["gender"];
            uie.Birthday = Request.Form["birthday"];
            uie.Address = Request.Form["address"];


            if (uie.UserId == -1)
            {
                if (sys_userBLL.AddEntity(uie) == 0)
                {
                    return Content("0");
                }
            }
            else 
            {
                sys_userBLL.UpdateEntity(uie);

            }

            return Content("1");
        }

        [HttpGet] 
        public ActionResult DeleteUser()
        {

            int id = Convert.ToInt32(Request.QueryString["userId"]);
            sys_userBLL.DeleteEntity(id);
            return Content("删除成功！");
        }

        public ActionResult UserRole(string id)
        {
            ViewData["UserId"] = id;
            return View();
        }


        public ActionResult GetUserRoles()
        {

            string strUserId=Request.QueryString["UserId"]??"-1";

            DataTable dtUserRoles = sys_userBLL.GetUserRoles(Convert.ToInt32(strUserId));

            string jsonUserRoles = JsonDataHelper.DataTableToJSON(dtUserRoles);

            return Content("{Rows:" + jsonUserRoles + "}");
        }

        public ActionResult UserRoleSelect()
        {
            Sys_RoleBLL sys_roleBLL=new Sys_RoleBLL();
            DataTable dtRoles = sys_roleBLL.GetAllRoles();

            string jsonRoles = JsonDataHelper.DataTableToJSON(dtRoles);

            ViewData["roles"] = "{Rows:" + jsonRoles + "}";

            return View();
        }

        public ActionResult SaveUserRoles()
        {
            string strResult="";
            string jsonRoles = Request.Params["Roles"] ?? "";
            string userId = Request.Params["UserId"] ?? "";

            DataTable dtUserRoles = JsonDataHelper.JSONToDataTable(jsonRoles);
            try
            {
                sys_userBLL.SaveUserRoles(Convert.ToInt32(userId), dtUserRoles);
            }
            catch (Exception ex)
            {

                strResult = ex.Message;
            }


            return Content(strResult);
        }

    }
}
